package cn.itcast.jsonParse;

import cn.itcast.beans.VehiclePlusBean;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * 复杂json的解析测试类
 * {"batteryAlarm": 0,"carMode": 1,"minVoltageBattery": 3.89,"chargeStatus": 1,"vin": "LS5A3CJC0JF890971","nevChargeSystemTemperatureDtoList": [{"probeTemperatures": [25, 23, 24, 21, 24, 21, 23, 21, 23, 21, 24, 21, 24, 21, 25, 21],"chargeTemperatureProbeNum": 16,"childSystemNum": 1}]}
 */
public class JsonParsePlusOptimizeDemo {
    public static void main(String[] args) {
        /**
         * 实现步骤：
         * 1）创建json字符串
         * 2）创建java类
         * 3）将使用org.json解析json字符串
         * 4）将json的字符串解析后为key：vaue，赋值给javaBean
         * 5）打印测试
         */
        //todo 1）创建json字符串
        String jsonStr = "{\"carMode\": 1,\"minVoltageBattery\": 3.89,\"chargeStatus\": 1,\"vin\": \"LS5A3CJC0JF890971\",\"nevChargeSystemTemperatureDtoList\": [{\"probeTemperatures\": [25, 23, 24, 21, 24, 21, 23, 21, 23, 21, 24, 21, 24, 21, 25, 21],\"chargeTemperatureProbeNum\": 16,\"childSystemNum\": 1}]}";

        //todo 2）创建java类
        VehiclePlusBean vehiclePlusBean = null;

        //todo 3）将使用org.json解析json字符串
        JSONObject jsonObject = new JSONObject(jsonStr);

        //todo 4）将json的字符串解析后为key：vaue，赋值给javaBean
        final HashMap<String, Object> vehicleHashMap = jsonToHashMap(jsonObject);
        int batteryAlarm = Integer.parseInt(vehicleHashMap.getOrDefault("batteryAlarm", -999999).toString());
        int carMode = Integer.parseInt(vehicleHashMap.getOrDefault("carMode", -999999).toString());
        double minVoltageBattery = Double.parseDouble(vehicleHashMap.getOrDefault("carMode", -999999D).toString());
        int chargeStatus  = Integer.parseInt(vehicleHashMap.getOrDefault("chargeStatus", -999999).toString());
        String vin = vehicleHashMap.getOrDefault("vin", -999999).toString();
        final String nevChargeSystemTemperatureDtoListStr = vehicleHashMap.getOrDefault("nevChargeSystemTemperatureDtoList", null).toString();
        final List<HashMap<String, Object>> nevChargeSystemTemperatureDtoList = jsonToList(nevChargeSystemTemperatureDtoListStr);
        final HashMap<String, Object> firstHashMap = nevChargeSystemTemperatureDtoList.get(0);
        int chargeTemperatureProbeNum = Integer.parseInt(firstHashMap.getOrDefault("chargeTemperatureProbeNum", -999999).toString());
        int childSystemNum = Integer.parseInt(firstHashMap.getOrDefault("childSystemNum", -999999).toString());
        List<Integer> probeTemperatures = new ArrayList<>();
        final String temperatures = firstHashMap.getOrDefault("probeTemperatures", null).toString();
        if(StringUtils.isNotEmpty(temperatures)){
            //[25, 23, 24, 21, 24, 21, 23, 21, 23, 21, 24, 21, 24, 21, 25, 21]
            final JSONArray jsonArray = new JSONArray(temperatures);
            //遍历集合中的每个元素
            for (Object obj : jsonArray) {
                probeTemperatures.add(Integer.parseInt(obj.toString()));
            }
        }
        vehiclePlusBean = new VehiclePlusBean(
                batteryAlarm,
                carMode,
                minVoltageBattery,
                chargeStatus,
                vin,
                probeTemperatures,
                chargeTemperatureProbeNum,
                childSystemNum
        );
        //todo 5）打印测试
        System.out.println(vehiclePlusBean);
    }

    /**
     * 将json字符串转换成集合对象
     * @param jsonStr
     * @return
     */
    private static List<HashMap<String, Object>> jsonToList(String jsonStr) {
        //定义需要返回的集合对象
        List<HashMap<String, Object>> list = new ArrayList<>();

        //将json字符串转换成jsonArray
        JSONArray jsonArray = new JSONArray(jsonStr);
        //遍历集合中的每个元素
        for (Object obj : jsonArray) {
            final String objStr = obj.toString();
            final JSONObject jsonObject = new JSONObject(objStr);
            list.add(jsonToHashMap(jsonObject));
        }

        //返回解析后的list集合
        return list;
    }

    /**
     * 将jsonObject对象转换成hashMap（将kv键值对的json解析后写入到map中）
     * @param jsonObject
     * @return
     */
    private static HashMap<String, Object> jsonToHashMap(JSONObject jsonObject) {
        //定义需要返回的hashMap对象
        HashMap<String, Object> kv = new HashMap<>();

        //获取到jsonObject对象的所有key
        final Set<String> keySet = jsonObject.keySet();

        //遍历集合，将每一个key对应的value查询出来写入到hashMap对象中
        for (String key : keySet) {
            kv.put(key, jsonObject.get(key));
        }

        //返回hashMap对象
        return kv;
    }
}
